﻿<%@ WebHandler Language="C#" Class="TaskDispenseInfo" %>

using System;
using System.Web;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using Chenxin.Lib.Utility;
using System.Web.Script.Serialization;

public class TaskDispenseInfo : AjaxHandlerBase
{
    public string jsonData = string.Empty;
    public string strUserID = string.Empty;
    public string strType = string.Empty;
    public string strRecordId = string.Empty;
    public string strOperate = string.Empty;
    
    //构造函数
    public TaskDispenseInfo()
    {
        //分配任务详情
        htTypePermit.Add("AllTask", Constants.Permit_DetailTask);
    }

    override public void RequestProcess(HttpContext context)
    { }
       /* string strOp = context.Request["op"];
        string strResponse = "";
        switch (strOp.ToLower())
        {
            case "update":
                strResponse = SearchTaskTable(context);
                break;
            case "update":
                strResponse = GetTasksBaseInfoAllByRecordId(context);
                break;
            case "update":
                strResponse = GetTasksBaseInfoAllByRecordId(context);
                break;
            case "update":
                strResponse = UpdateMyTask(context);
                break;
            case "update":
                strResponse = UpdateAllTask(context);
                break;
            case "cancleprotect":
                strResponse = DeleteTaskTable(context);
                break; 
            case "cancleprotect":
                strResponse = AddTaskTable(context);
                break;
            default:
                break;
        }
        context.Response.Write(strResponse);
    }

    /// <summary>
    /// 显示任务列表(得到一个Task的JSONData)
    /// </summary>
    /// <param name="context"></param>
    public string InitTaskTable(HttpContext context)
    {
        //分页
        int total_page = 0;
        int count = 0;
        int rows = int.Parse(HttpContext.Current.Request.QueryString["rows"]);
        int page = int.Parse(HttpContext.Current.Request.QueryString["page"]);

        //页码
        int PageIndex = int.Parse(HttpContext.Current.Request.QueryString["rows"]);
        //页显示行数
        int PageSize = int.Parse(HttpContext.Current.Request.QueryString["page"]);
        //排序
        string OrderField = HttpContext.Current.Request.QueryString["sidx"] + " " + HttpContext.Current.Request.QueryString["sord"];
        //条件
        string StrWhere = "1=1 ";

        //if (HttpContext.Current.Request.QueryString["StrWhere"] != null)
        StrWhere += Convert.ToString(HttpContext.Current.Request.QueryString["StrWhere"]);
        
        switch (strType.Trim())
        {
            case "AllTask":
                StrWhere += "and AddPersonId = '" + new Guid(strUserID) + "'";
                break;
            case "MyTask":
                StrWhere += "and taskReceiverId = '" + new Guid(strUserID) + "'";
                break;
            default:
                break;
        }

        //欲选择字段列表 
        string FieldList = "RecordId,Title,relatedCaption,relatedCode,TaskType,TaskBeginDay,TaskEndDay,Status ";
        //子查询欲选择字段列表
        string TableFieldList = "RecordId,Title,relatedCode,relatedCaption,TaskType,Convert(varchar(10),TaskBeginDay,120) as TaskBeginDay,Convert(varchar(10),TaskEndDay,120) as TaskEndDay, case Status when 0 then '待处理' when 1 then '已处理' end as Status";

        //所要查询的表名
        string TableName = "Tasks_BaseInfo";
        //根据sql执行查询
        DataSet ds = Chenxin.Lib.House.DAL.Tasks_BaseInfo.GetList(PageIndex, PageSize, OrderField, TableName, StrWhere, FieldList, TableFieldList, true);

        count = (int)ds.Tables[1].Rows[0][0];
        //数据开始转换
        DataTable dt = ds.Tables[0];
        // dt.Load(dr);
        if (count > 0)
            total_page = count / rows;
        if (page > total_page)
            page = int.Parse(total_page.ToString());

        //转成json数据,这里是关键的函数此方法我是放入的一个JSON操作类中的
        //jsonData = GetJson(dt, rows, page, count);

        context.Response.Write(jsonData);   
    }
    
    /// <summary>
    /// 通过任务ID取到此任务的详细信息
    /// </summary>
    /// <param name="context"></param>
    public string GetTasksBaseInfoAllByRecordId(HttpContext context)
    {
        try
        {
            StringBuilder strHtml = new StringBuilder();

            string strWhere = "RecordId = '" + new Guid(strRecordId) + "'";

            List<Chenxin.Lib.House.Entity.Tasks_BaseInfo> lsTasksInfo = Chenxin.Lib.House.DAL.Tasks_BaseInfo.GetListArray(strWhere);

            foreach (Chenxin.Lib.House.Entity.Tasks_BaseInfo lsTasks in lsTasksInfo)
            {
                strHtml.Append("<dd>");
                strHtml.Append(" <input type='hidden' name='hiddrelatedCode' value='" + lsTasks.relatedCode + "' />");
                strHtml.Append(" <input type='hidden' name='hiddrelatedCaption' value='" + lsTasks.relatedCaption + "' />");
                strHtml.Append(" <input type='hidden' name='hiddReceiverDepart' value='" + lsTasks.ReceiverDepart + "' />");
                strHtml.Append(" <input type='hidden' name='hiddtaskReceiver' value='" + lsTasks.taskReceiver + "' />");
                strHtml.Append(" <input type='hidden' name='hiddTaskType' value='" + lsTasks.TaskType + "' />");
                strHtml.Append(" <input type='hidden' name='hiddTitle' value='" + lsTasks.Title + "' />");
                strHtml.Append(" <input type='hidden' name='hiddTaskBeginDay' value='" +Convert.ToDateTime(lsTasks.TaskBeginDay).ToString("yyyy-MM-dd") + "' />");
                strHtml.Append(" <input type='hidden' name='hiddTaskEndDay' value='" + Convert.ToDateTime(lsTasks.TaskEndDay).ToString("yyyy-MM-dd") + "' />");
                strHtml.Append(" <input type='hidden' name='hiddDescription' value='" + lsTasks.Description + "' />");
                string status = string.Empty;
                if (lsTasks.Status == 0)
                    status = "未处理";
                else if (lsTasks.Status == 1)
                    status = "已处理";
                strHtml.Append(" <input type='hidden' name='hiddStatus' value='" + status + "' />");
                strHtml.Append(" <input type='hidden' name='hiddStatusno' value='" + lsTasks.Status + "' />");
                if (lsTasks.DealDay == null)
                    strHtml.Append(" <input type='hidden' name='hiddDealDay' value='" + DateTime.Now.ToString("yyyy-MM-dd") + "' />");
                else
                strHtml.Append(" <input type='hidden' name='hiddDealDay' value='" + Convert.ToDateTime(lsTasks.DealDay).ToString("yyyy-MM-dd") + "' />");
                
                strHtml.Append(" <input type='hidden' name='hiddDealDescription' value='" + lsTasks.DealDescription + "' />");
                
                strHtml.Append("</dd>");
            }

            context.Response.Write(retExecResultT(Constants.ExecResult.Succ, "OK", strHtml.ToString()));
            
        }
        catch (Exception ex)
        {

            context.Response.Write(retExecResultT(Constants.ExecResult.Error, ex.Message, ""));
        }
           
    }

    /// <summary>
    /// 更新我的任务状态
    /// </summary>
    /// <param name="context"></param>
    public string UpdateMyTask(HttpContext context)
    {
        try
        {
            string data = context.Request.Params["data"];

            JavaScriptSerializer jssr = new JavaScriptSerializer();
            Dictionary<string, object> jsonTaskDispenseInfo = (Dictionary<string, object>)jssr.DeserializeObject(data);
            
            Chenxin.Lib.House.Entity.Tasks_BaseInfo clhet = Chenxin.Lib.House.DAL.Tasks_BaseInfo.GetModel(new Guid(strRecordId));
            
            clhet.Status = Convert.ToInt32(jsonTaskDispenseInfo["_thestatus"]);
            clhet.DealDay = Convert.ToDateTime(jsonTaskDispenseInfo["_theendaddtime"]);
            clhet.DealDescription = Convert.ToString(jsonTaskDispenseInfo["_thestatedescribe"]); 
            
            context.Response.Write(retExecResult(Constants.ExecResult.Succ, Chenxin.Lib.House.BLL.Tasks_BaseInfo.Update(clhet), ""));
        
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }    
    }

    /// <summary>
    /// 更新分配任务状态
    /// </summary>
    /// <param name="context"></param>
    public string UpdateAllTask(HttpContext context)
    {
        try
        {
            string data = context.Request.Params["data"];

            JavaScriptSerializer jssr = new JavaScriptSerializer();
            Dictionary<string, object> jsonTaskDispenseInfo = (Dictionary<string, object>)jssr.DeserializeObject(data);

            Chenxin.Lib.House.Entity.Tasks_BaseInfo clhet = Chenxin.Lib.House.DAL.Tasks_BaseInfo.GetModel(new Guid(strRecordId));

            clhet.Title = Convert.ToString(jsonTaskDispenseInfo["_thetxttitle2"]);
            clhet.TaskType = Convert.ToString(jsonTaskDispenseInfo["_thedivtasktype2"]);
            clhet.TaskBeginDay = Convert.ToDateTime(jsonTaskDispenseInfo["_theinputtaskbeginday2"]);
            clhet.TaskEndDay = Convert.ToDateTime(jsonTaskDispenseInfo["_theinputtaskendday2"]);
            clhet.Description = Convert.ToString(jsonTaskDispenseInfo["_thetxtdescription"]);
            clhet.Status = Convert.ToInt32(jsonTaskDispenseInfo["_thehidstatus2"]);
            clhet.DealDay = Convert.ToDateTime(jsonTaskDispenseInfo["_theendaddtime2"]);
            clhet.DealDescription = Convert.ToString(jsonTaskDispenseInfo["_thetxtresult"]);

            context.Response.Write(retExecResult(Constants.ExecResult.Succ, Chenxin.Lib.House.BLL.Tasks_BaseInfo.Update(clhet), ""));
        
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }

    /// <summary>
    /// 添加任务信息
    /// </summary>
    /// <param name="context"></param>
    public string AddTaskTable(HttpContext context)
    {

        string data = context.Request.Params["data"];

        JavaScriptSerializer jssr = new JavaScriptSerializer();
        Dictionary<string, object> jsonTaskDispenseInfo = (Dictionary<string, object>)jssr.DeserializeObject(data);

        Chenxin.Lib.House.Entity.Tasks_BaseInfo clhet = new Chenxin.Lib.House.Entity.Tasks_BaseInfo();
        try
        {
            clhet.RecordId = Guid.NewGuid();
            clhet.relatedId = new Guid(jsonTaskDispenseInfo["_houseid"].ToString());
            clhet.relatedCaption = Convert.ToString(jsonTaskDispenseInfo["_address"]);
            clhet.relatedCode = Convert.ToString(jsonTaskDispenseInfo["_housecode"]);
            clhet.taskReceiverId = new Guid(jsonTaskDispenseInfo["_taskuserid"].ToString());
            clhet.taskReceiver = Convert.ToString(jsonTaskDispenseInfo["_taskuser"]);
            clhet.ReceiverDepartId = new Guid(jsonTaskDispenseInfo["_departmentid"].ToString());
            clhet.ReceiverDepart = Convert.ToString(jsonTaskDispenseInfo["_department"]);
            clhet.TaskType = Convert.ToString(jsonTaskDispenseInfo["_tasktype"]);
            clhet.TaskBeginDay = Convert.ToDateTime(jsonTaskDispenseInfo["_startaddtime"]);
            clhet.TaskEndDay = Convert.ToDateTime(jsonTaskDispenseInfo["_endaddTime"]);
            clhet.Status = 0;
            clhet.Title = Convert.ToString(jsonTaskDispenseInfo["_title"]);
            clhet.Description = Convert.ToString(jsonTaskDispenseInfo["_describe"]);
            clhet.Addtime = DateTime.Now;
            clhet.AddPersonId = new Guid(jsonTaskDispenseInfo["_userid"].ToString());
            clhet.AddPerson = Convert.ToString(jsonTaskDispenseInfo["_username"]);
            clhet.AddDepartId = new Guid(jsonTaskDispenseInfo["_departid"].ToString());
            clhet.AddDepart = Convert.ToString(jsonTaskDispenseInfo["_departname"]);
            clhet.AddCompanyId = new Guid(jsonTaskDispenseInfo["_taskcompanyid"].ToString());
            clhet.AddCompany = Convert.ToString(jsonTaskDispenseInfo["_taskcompanyname"]);
            clhet.CityId = new Guid(jsonTaskDispenseInfo["_taskcityid"].ToString());
            clhet.CityName = Convert.ToString(jsonTaskDispenseInfo["_taskcityname"]);
            
            context.Response.Write(retExecResult(Constants.ExecResult.Succ, Chenxin.Lib.House.BLL.Tasks_BaseInfo.Add(clhet), ""));

        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
  
    }
    
    /// <summary>
    /// 删除任务信息
    /// </summary>
    /// <param name="context"></param>
    public string DeleteTaskTable(HttpContext context)
    {
        try
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Succ, Chenxin.Lib.House.BLL.Tasks_BaseInfo.Delete(new Guid(strRecordId)), ""));
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }  
    }

    /// <summary>
    /// 查询任务信息
    /// </summary>
    /// <param name="context"></param>
    public string SearchTaskTable(HttpContext context)
    {
        InitTaskTable(context);
    }
 */
    override public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}